<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="订单编号:" prop="order_No">
          <el-input v-model="formData.order_No" :clearable="true"  placeholder="请输入订单编号" />
       </el-form-item>
        <el-form-item label="用户ID:" prop="user_ID">
          <el-input v-model="formData.user_ID" :clearable="true"  placeholder="请输入用户ID" />
       </el-form-item>
        <el-form-item label="服务项目:" prop="service_Name">
          <el-input v-model="formData.service_Name" :clearable="true"  placeholder="请输入服务项目" />
       </el-form-item>
        <el-form-item label="服务时间段:" prop="service_Time">
          <el-input v-model="formData.service_Time" :clearable="true"  placeholder="请输入服务时间段" />
       </el-form-item>
        <el-form-item label="服务地址:" prop="service_Addr">
          <el-input v-model="formData.service_Addr" :clearable="true"  placeholder="请输入服务地址" />
       </el-form-item>
        <el-form-item label="经度:" prop="service_Lng">
          <el-input v-model="formData.service_Lng" :clearable="true"  placeholder="请输入经度" />
       </el-form-item>
        <el-form-item label="纬度:" prop="service_Lat">
          <el-input v-model="formData.service_Lat" :clearable="true"  placeholder="请输入纬度" />
       </el-form-item>
        <el-form-item label="项目金额:" prop="service_Amount">
          <el-input-number v-model="formData.service_Amount" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="出行费用:" prop="taxi_Amount">
          <el-input-number v-model="formData.taxi_Amount" :precision="2" :clearable="true"></el-input-number>
       </el-form-item>
        <el-form-item label="客户ID:" prop="client_ID">
          <el-input v-model="formData.client_ID" :clearable="true"  placeholder="请输入客户ID" />
       </el-form-item>
        <el-form-item label="订单状态:" prop="status">
           <el-select v-model="formData.status" placeholder="请选择订单状态" style="width:100%" :clearable="true" >
              <el-option v-for="(item,key) in orderStatusOptions" :key="key" :label="item.label" :value="item.value" />
           </el-select>
       </el-form-item>
        <el-form-item label="备注:" prop="remark">
          <el-input v-model="formData.remark" :clearable="true"  placeholder="请输入备注" />
       </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {
  createOrders,
  updateOrders,
  findOrders
} from '@/api/pkgOrder/orders'

defineOptions({
    name: 'OrdersForm'
})

// 自动获取字典
import { getDictFunc } from '@/utils/format'
import { useRoute, useRouter } from "vue-router"
import { ElMessage } from 'element-plus'
import { ref, reactive } from 'vue'

const route = useRoute()
const router = useRouter()

const type = ref('')
const orderStatusOptions = ref([])
const formData = ref({
            order_No: '',
            user_ID: '',
            service_Name: '',
            service_Time: '',
            service_Addr: '',
            service_Lng: '',
            service_Lat: '',
            service_Amount: 0,
            taxi_Amount: 0,
            client_ID: '',
            status: '',
            remark: '',
        })
// 验证规则
const rule = reactive({
})

const elFormRef = ref()

// 初始化方法
const init = async () => {
 // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
    if (route.query.id) {
      const res = await findOrders({ ID: route.query.id })
      if (res.code === 0) {
        formData.value = res.data.reorders
        type.value = 'update'
      }
    } else {
      type.value = 'create'
    }
    orderStatusOptions.value = await getDictFunc('orderStatus')
}

init()
// 保存按钮
const save = async() => {
      elFormRef.value?.validate( async (valid) => {
         if (!valid) return
            let res
           switch (type.value) {
             case 'create':
               res = await createOrders(formData.value)
               break
             case 'update':
               res = await updateOrders(formData.value)
               break
             default:
               res = await createOrders(formData.value)
               break
           }
           if (res.code === 0) {
             ElMessage({
               type: 'success',
               message: '创建/更改成功'
             })
           }
       })
}

// 返回按钮
const back = () => {
    router.go(-1)
}

</script>

<style>
</style>
